/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.feed.impl;

import java.io.IOException;
import java.io.OutputStream;

import static com.semagia.atomico.IConstants.REL_ALTERNATE;

import com.semagia.atomico.MediaType;
import com.semagia.atomico.server.feed.IOutputAwareFeedHandler;

import static com.semagia.atomico.server.feed.impl.FeedWriterUtils.writeFeedStart;
import static com.semagia.atomico.server.feed.impl.FeedWriterUtils.writeEntryStart;

/**
 * {@link IFeedWriter} which serializes a snapshot feed.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
final class SnapshotsFeedWriter implements IFeedWriter {

    private final ISnapshotsFeed _feed;
    private final IOutputAwareFeedHandler _handler;

    public SnapshotsFeedWriter(final ISnapshotsFeed feed,
            final IOutputAwareFeedHandler handler) {
        _feed = feed;
        _handler = handler;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IWritable#write(java.io.OutputStream)
     */
    @Override
    public void write(final OutputStream out) throws IOException {
        _handler.init(out);
        writeFeedStart(_handler, _feed);
        for (ISnapshotsFeed.ISnapshotEntry entry: _feed.getEntries()) {
            writeEntryStart(_handler, entry);
            for (MediaType mediaType: entry.getMediaTypes()) {
                _handler.link(entry.getLink(), REL_ALTERNATE, mediaType.toString());
            }
            _handler.endEntry();
        }
        _handler.endFeed();
    }

}
